﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.Model;
using AvatarHelper;

public partial class Admin_PositionCategory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            btn_Add.Visible = true;
            panel_Add.Visible = false;
            panel_Edit.Visible = false;
        }
    }
    protected void btn_DUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                Button _b = sender as Button;
                int _id = Convert.ToInt32(_b.CommandArgument);
                var _d = _adc.Position.Where(p => p.ID == _id).FirstOrDefault();
                if (_d == null) throw new Exception("职位分类不存在");

                txt_NameUpdate.Text = _d.Name;
                btn_DUpdateSubmit.CommandArgument = _d.ID.ToString();

                btn_Add.Visible = false;
                panel_Add.Visible = false;
                panel_Edit.Visible = true;
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "出错:" + _e.Message;
        }
    }
    protected void btn_DAddSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                var _name = string.IsNullOrEmpty(txt_NameAdd.Text) ? string.Empty : txt_NameAdd.Text;
                if (string.IsNullOrEmpty(_name)) throw new Exception("分类名称不能为空");

                var _position = _adc.Position.Where(p => p.Name == _name).FirstOrDefault();
                if (
                    _position != null) throw new Exception("已存在相同名称分类");

                _position = new Position();
                _position.Name = _name;

                _adc.Position.InsertOnSubmit(_position);
                _adc.SubmitChanges();

                GridView1.DataBind();
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "添加职位分类错误:" + _e.Message;
        }
    }
    protected void btn_DDelete_Click(object sender, EventArgs e)
    {
        try
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                Button _b = sender as Button;
                int _id = Convert.ToInt32(_b.CommandArgument);

                var _position=_adc.Position.Where(p=>p.ID==_id).FirstOrDefault();
                if (_position == null) throw new Exception("职位分类不存在");
                
                _adc.Position.DeleteOnSubmit(_position);
                _adc.SubmitChanges();

                Response.Redirect(Request.Url.ToString());
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void btn_DUpdateSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (string.IsNullOrEmpty(txt_NameUpdate.Text)) throw new Exception("职位分类名称不能为空");
            using (var _adc = DBHelper.GetDataContext())
            {
                Button _b = sender as Button;
                int _id = Convert.ToInt32(_b.CommandArgument);

                var _position = _adc.Position.Where(p => p.ID == _id).FirstOrDefault();
                if (_position == null) throw new Exception("职位分类不存在");

                _position.Name = txt_NameUpdate.Text;

                _adc.SubmitChanges();

                Response.Redirect(Request.Url.ToString());
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void btn_DUpdateCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.ToString());
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        btn_Add.Visible = false;
        panel_Add.Visible = true;
        panel_Edit.Visible = false;
    }
}
